﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using System.Collections.ObjectModel;
using System.IO;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Globalization;

namespace University.Workshop.WebApp
{
	public partial class RegisterUser : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				calExtender.StartDate = DateTime.Now.AddYears(-100);
				calExtender.EndDate = DateTime.Now.AddYears(-18);
                calExtender.SelectedDate = DateTime.Now.AddYears(-18);
			}												   
		}        

		protected void BtnSubimt_Click(object sender, EventArgs e)
		{
			LbFirstName.Visible = false;
			LbLastName.Visible = false;
			LbBirthday.Visible = false;
			LbEmail.Visible = false;
			LbPass.Visible = false;
			LbRePass.Visible = false;
			LbPhone.Visible = false;
			LbMobilePhone.Visible = false;
			LbCity.Visible = false;
			LbAllBlank.Visible = false;
			try
			{
				Collection<RegisterError> errorList = UserRegistrationManager.RegisterUser(TbFirstName.Text, TbLastName.Text, (rMale.Checked) ? 'M' : 'F', DateTime.Parse(calendarBox.Text,CultureInfo.CurrentCulture), TbEmail.Text, TbPass.Text, TbRePass.Text, CbDepartment.SelectedValue.ToString(), TbCity.Text, TbMobile.Text, TbPhone.Text);
				if (errorList.Count > 0)
				{
					foreach (var error in errorList)
					{
						switch (error)
						{
							case RegisterError.BlankNull:
								{
									LbAllBlank.Visible = true;
									break;
								}
							case RegisterError.FName:
								{
									LbFirstName.Visible = true;
									break;
								}
							case RegisterError.LName:
								{
									LbLastName.Visible = true;
									break;
								}
							case RegisterError.BirthNull:
								{
									LbBirthday.Visible = true;
									calendarBox.Text = "";
									break;
								}
							case RegisterError.FormatEmail:
								{
									LbEmail.Text = "Wrong email format";
									LbEmail.Visible = true;
									break;
								}
							case RegisterError.BusinessEmail:
								{
									LbEmail.Text = "The requested email is already registered";
									LbEmail.Visible = true;
									break;
								}
							case RegisterError.FormatPass:
								{
									LbPass.Visible = true;
									break;
								}
							case RegisterError.REPass:
								{
									LbRePass.Visible = true;
									break;
								}
							case RegisterError.City:
								{
									LbCity.Visible = true;
									break;
								}
							case RegisterError.Mobile:
								{
									LbMobilePhone.Visible = true;
									break;
								}
							case RegisterError.Phone:
								{
									LbPhone.Visible = true;
									break;
								}
						}
					}
				}
				else
				{
					Collection<HttpPostedFile> images = new Collection<HttpPostedFile>();
					HttpFileCollection uploadFilCol = Request.Files;
					for (int i = 0; i < uploadFilCol.Count; i++)
					{
						HttpPostedFile file = uploadFilCol[i];
						images.Add(file);
					}
					images = ProductRegistrationManager.GetUploadedFiles(images);
					ImageUploadHandler.UploadUserImages(images, Server,UserDataAccess.GetUserByEmail(TbEmail.Text).Id);
					Session["newUserToLogin"] = TbEmail.Text;
					Response.Write("<script>alert('User registered Sucessfully!'); window.location.href = 'Login.aspx';</script>");
				}
			}
			catch (Exception exc)
			{
				Response.Write("<script>alert('" + exc.Message + "'); window.location.href = 'Default.aspx';</script>");
			}
		}
	}
}
